<template>
  <div class="login-page loginPage" id="loginPage">
    <div class="login-center">
      <div class="project-name">项目信息管理系统</div>
      <div class="login-box">
        <div class="title-container">用户登录</div>
        <div class="form-container">
          <el-form
            ref="loginForm"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
            auto-complete="on"
            label-position="left"
          >
            <el-form-item prop="username">
              <span class="svg-container">
                <i class="fa fa-user"></i>
              </span>
              <el-input
                ref="username"
                v-model="loginForm.username"
                placeholder="用户名"
                name="username"
                type="text"
                tabindex="1"
                auto-complete="on"
              />
            </el-form-item>
            <el-form-item prop="password">
              <span class="svg-container">
                <i class="fa fa-key"></i>
              </span>
              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                placeholder="密码"
                name="password"
                tabindex="2"
                auto-complete="on"
                @keyup.enter.native="handleLogin"
              />
              <span class="show-pwd" @click="showPwd">
                <svg-icon
                  :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
                />
              </span>
            </el-form-item>
          </el-form>
          <el-button
            :loading="loading"
            type="primary"
            @click.native.prevent="handleLogin"
            >登录</el-button
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Cookies from "js-cookie";
import { validUsername } from "@/utils/validate";
import { loginService } from "@/api/loginService";

export default {
  name: "Login",
  data() {
    //          const validateUsername = (rule, value, callback) => {
    //              if (!validUsername(value)) {
    //                  callback(new Error('Please enter the correct user name'))
    //              } else {
    //                  callback()
    //              }
    //          }
    //          const validatePassword = (rule, value, callback) => {
    //              if (value.length < 6) {
    //                  callback(new Error('The password can not be less than 6 digits'))
    //              } else {
    //                  callback()
    //              }
    //          }
    return {
      loginForm: {
        username: "admin",
        password: "admin"
      },
      loginRules: {
        //   username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        username: [
          { required: true, trigger: "blur", message: "请输入用户名" }
        ],
        password: [{ required: true, trigger: "blur", message: "请输入密码" }]
      },
      loading: false,
      passwordType: "password",
      redirect: undefined
    };
  },
  mounted() {
    this.particleCanvas();
  },

  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$router.push({ path: "/one" });
    },
    //粒子背景效果
    particleCanvas() {
      function o(w, v, i) {
        return w.getAttribute(v) || i;
      }

      function j(i) {
        return document.getElementsByTagName(i);
      }

      function l() {
        var i = j("script"),
          w = i.length,
          v = i[w - 1];
        return {
          l: w,
          z: o(v, "zIndex", -1),
          o: o(v, "opacity", 0.8),
          c: o(v, "color", "200,200,244"),
          n: o(v, "count", 80)
        };
      }

      function k() {
        (r = u.width =
          window.innerWidth ||
          document.documentElement.clientWidth ||
          document.body.clientWidth),
          (n = u.height =
            window.innerHeight ||
            document.documentElement.clientHeight ||
            document.body.clientHeight);
      }

      function b() {
        e.clearRect(0, 0, r, n);
        var w = [f].concat(t);
        var x, v, A, B, z, y;
        t.forEach(function(i) {
          (i.x += i.xa),
            (i.y += i.ya),
            (i.xa *= i.x > r || i.x < 0 ? -1 : 1),
            (i.ya *= i.y > n || i.y < 0 ? -1 : 1),
            e.fillRect(i.x - 0.5, i.y - 0.5, 1, 1);
          for (v = 0; v < w.length; v++) {
            x = w[v];
            if (i !== x && null !== x.x && null !== x.y) {
              (B = i.x - x.x), (z = i.y - x.y), (y = B * B + z * z);
              y < x.max &&
                (x === f &&
                  y >= x.max / 2 &&
                  ((i.x -= 0.05 * B), (i.y -= 0.05 * z)),
                (A = (x.max - y) / x.max),
                e.beginPath(),
                (e.lineWidth = A),
                // (e.lineWidth = A / 2),
                (e.strokeStyle = "rgba(" + s.c + "," + (A + 0.5) + ")"),
                e.moveTo(i.x, i.y),
                e.lineTo(x.x, x.y),
                e.stroke());
            }
          }
          w.splice(w.indexOf(i), 1);
        }),
          m(b);
      }

      var u = document.createElement("canvas"),
        s = l(),
        c = "c_n" + s.l,
        e = u.getContext("2d"),
        r,
        n,
        m =
          window.requestAnimationFrame ||
          window.webkitRequestAnimationFrame ||
          function(i) {
            window.setTimeout(i, 1000 / 45);
          },
        a = Math.random,
        f = { x: null, y: null, max: 20000 };
      u.id = c;
      u.style.cssText = "position:fixed;top:0;left:0;opacity:" + s.o;
      document.getElementById("loginPage").appendChild(u);
      k(), (window.onresize = k);
      (window.onmousemove = function(i) {
        (i = i || window.event), (f.x = i.clientX), (f.y = i.clientY);
      }),
        (window.onmouseout = function() {
          (f.x = null), (f.y = null);
        });
      for (var t = [], p = 0; s.n > p; p++) {
        var h = a() * r,
          g = a() * n,
          q = 2 * a() - 1,
          d = 2 * a() - 1;
        t.push({ x: h, y: g, xa: q, ya: d, max: 6000 });
      }
      setTimeout(function() {
        b();
      }, 100);
    }
  }
};
</script>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
$bg:#0b0f2a;
$bg2:#0b0f2a;
.login-page {
  /deep/{
    .el-button--primary {
    color: #FFFFFF;
    background-color: $bg2;
    border-color: #1890ff;
}
input{
      background: $bg2;
    color: #fff;
}
  }
  display: flex;
  width: 100%;
  justify-content: center;
  align-items: center;
  // background-color: $bg;
  height: 100%;
  // background: url(../../assets/images/login/bf.jpg) top left no-repeat;
  background-size: cover;
  overflow: hidden;
  background-image: 
  radial-gradient(ellipse at 50% 50%, #0b0f2a 10%, #22284b 88%);
  .login-center {
    width: 460px;
    height: 400px;
    z-index: 2;
  }

  .project-name {
    height: 90px;
    color: #fff;
    font-size: 28px;
    line-height: 90px;
    text-align: center;
  }
  .login-box {
    position: relative;
    height: 310px;
    background-color: $bg2;
    border-radius: 5px;
    .form-container {
      padding: 0px 40px;
    }
    .title-container {
      height: 55px;
      text-align: center;
      color: #fff;
      font-size: 22px;
      background: $bg2;
      line-height: 55px;
      border-bottom: 1px solid $bg2;
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>

<style lang="scss">
$bg: #283443;
$light_gray: black;
$cursor: black;

// @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
//     .login-page .el-input input {
//         color: $cursor;
//     }
// }

/* reset element-ui css */
.login-page {
  .login-form {
    margin-top: 20px;
  }
  .el-button {
    width: 350px;
    height: 43px;
  }
  .el-form-item {
    border: 1px solid #cccccc;
    border-radius: 5px;
    color: #454545;
    width: 350px;
    height: 43px;
  }
  .el-form-item__content {
    height: 43px;
    line-height: 34px;
  }
  .el-input {
    width: 85%;
    height: 34px;
  }
  .el-input__inner {
    border: 0;
  }
}
</style>
